import pyclipper
import math
import matplotlib.pyplot as plt
import numpy as np
from loadXML import loadXMLFile

def show():
    ax = plt.gca()
    ax.invert_yaxis()
    plt.axis ('equal')
    plt.show()

def plotPoly(rings):
    for ring in rings:
        ring = np.reshape(ring, (len(ring), 2))
        x = ring[:,0].tolist()
        y = ring[:,1].tolist()
        x.append(x[0])
        y.append(y[0])
        plt.plot(x, y, '-')
    # plt.show()



from point import *

def buildBias(outer, inner):
    # outer, inner = loadXMLFile(file_name)
    list = []
    debug_list = []
    N = len(outer)
    for i in range(N):
        if intersect(outer[i],inner[i],outer[(i+1)%N],inner[(i+1)%N]):
            list.append(outer[i])
            # list.append(inner[i])
            list.append(inner[(i+1)%N])
            list.append(inner[i])

        else: 
            o1 = Point(outer[i])
            o2 = Point(outer[(i+1)%N])
            
            i1 = Point(inner[i])
            i2 = Point(inner[(i+1)%N])
            o1o2 = o2.subtract(o1)
            si1 = cross_product(o1o2, i1.subtract(o1))
            si2 = cross_product(o1o2, i2.subtract(o2))
            
            if (si1 * si2 >= 0): # same bias dir
                list.append(outer[i])
                list.append(inner[i])
                list.append(inner[(i+1)%N])
                continue
            
            # different bias dir
            if cross_product(o1o2, i2.subtract(o1)) * cross_product(i1.subtract(o1), i2.subtract(o1)) < 0:
                list.append(outer[i])
                # list.append(inner[i])
                list.append(inner[(i+1)%N])
                print("=======")
                continue
            
            if cross_product(o1o2, i2.subtract(o2)) * cross_product(o2.subtract(i1), i2.subtract(o2)) < 0:
                list.append(outer[i])
                list.append(inner[i])
                # list.append(inner[(i+1)%N])
                print("=======")
                continue
            list.append(outer[i])
            list.append(inner[i])
            list.append(inner[(i+1)%N])
                
            
            
    return list

list = []

# clip4 may need special handle
outer, inner = loadXMLFile('./data/clip4.xml')
# outer, inner = outer[::-1], inner[::-1]

# outer, inner = [[0,0], [20, 0], [10,10]], [[20,-1], [0, -1], [10, -10]]

list = buildBias(outer, inner)



plotPoly([list])
# show()

def clean(ring):
    pc = pyclipper.Pyclipper()
    clip = ((-1000, -1000), (1000, -1000), (1000, 1000), (-1000, 1000))
    pc.AddPath(clip, pyclipper.PT_CLIP, True)
    pc.AddPaths([ring], pyclipper.PT_SUBJECT, True)

    # PFT_NEGATIVE
    # PFT_POSITIVE
    # solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_POSITIVE, pyclipper.PFT_NEGATIVE) # ok
    solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_POSITIVE, pyclipper.PFT_POSITIVE)

    # print(solution)
    return solution



plotPoly(clean(list))


# show()

plotPoly([inner])


# show()

plotPoly(clean(inner))


show()
